﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class CS_YS_Query : BasePage
    {
        CS_YS_Dao dao = new CS_YS_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                this.lbTitle.Text = "颜色列表";
                BindGridview();
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            GridView1.PageIndex = RestorePageIndex();

            List<CS_YS_Ex> listEx = new List<CS_YS_Ex>();

            CS_YSLX_Dao yslxDao = new CS_YSLX_Dao();
            List<CS_YSLX> listLX = yslxDao.QueryAll();

            List<CS_YS> list = dao.QueryAll();
            CS_YS_Ex ce = null;
            foreach(CS_YS c in list)
            {
                ce = new CS_YS_Ex();
                ce.YSLXBH = c.YSLXBH;
                CS_YSLX lx = listLX.Find(delegate(CS_YSLX p) { return p.BH == c.YSLXBH; });
                ce.YSLXMC = lx.MC;
                ce.BH = c.BH;
                ce.MC = c.MC;

                listEx.Add(ce);
            }

            this.GridView1.DataSource = listEx;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((ImageButton)(e.Row.Cells[3].Controls[0])).Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = dao.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/CS_YS_Add.aspx");
        }

        protected class CS_YS_Ex
        {
            public string YSLXBH { get; set; }
            public string YSLXMC { get; set; }
            public string BH { get; set; }
            public string MC { get; set; }
        }
    }
}